<template>
    <el-dialog :title="title" :visible.sync="visible" :before-close="closeDialog">
        <el-form :model="form" :rules="rules" ref="ruleForm" :label-width="formLabelWidth" size="small">
            <el-form-item v-if="title=='新增用户'" label="账号" prop="username">
                <el-input v-model="form.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="性别">
                <el-radio-group v-model="form.sex">
                    <el-radio label="男" value="0"></el-radio>
                    <el-radio label="女" value="1"></el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="角色" prop="roleNameIdList">
                <el-select v-model="form.roleNameIdList" placeholder="请选择角色" multiple>
                    <el-option v-for="item in roleNameList" :key="item.roleId" :label="item.roleName" :value="item.roleId"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="closeDialog">取 消</el-button>
            <el-button type="primary" @click="comitDialog">确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
import { addUser ,updateUser} from "@/api/modules/SystemManagement/userManagement";
export default {
    props: {
        title: String,
        visible: {
            type: Boolean,
            default: false
        },
        form:{
            type: Object
        },
        roleNameList:{
            type:Array
        }
    },
    watch: {
        visible(newdata) {
            if(!newdata){
                this.resetForm("ruleForm");
            }
        }
    },
    created(){

    },
    data() {
        return {
            formLabelWidth: '80px',
            rules: {
                username:[
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                ],
                roleNameIdList:[
                    { required: true, message: '请至少选择一个角色', trigger: 'change' }
                ]

            },
        }
    },
    methods: {
        closeDialog(){
            this.$emit("closeDialog")
        },
        comitDialog(){
            this.submitForm("ruleForm");
        },
        operationFrom(){
          this.$emit("operationFrom")
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // alert('submit!');
                    if(this.title == "新增用户"){
                        let data = JSON.parse(JSON.stringify(this.form));
                        addUser(data).then(()=>{
                            this.operationFrom();
                        })
                    }else{
                        let data = JSON.parse(JSON.stringify(this.form));
                        updateUser(data).then(()=>{
                            this.operationFrom();
                        })
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>
<style scoped>
.el-select{
    width: 100%;
}
</style>